﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BlackBox.Models
{
    public class ScoreRepository
    {
        DataClassesDataContext db = new DataClassesDataContext();

        #region SingleTon patern
        public static GameRepository instance;


        public static GameRepository Instance
        {
            get
            {
                if (instance == null)
                    instance = new GameRepository();
                return instance;
            }
        }
        #endregion 

        #region Get Score By Game

        public ScoreBoard getScoreByGame(int Userid, int Gameid)
        {
            ScoreToUserToGame fromDb = (from x in db.ScoreToUserToGames
                                        where x.userid == Userid && x.gameid == Gameid
                                        select x).Single();
            ScoreBoard ReturnScore = (from x in db.ScoreBoards
                                    where x.id == fromDb.scoreid
                                    select x).Single();
            return ReturnScore;
        }
        #endregion

        #region Get High Score
        public IEnumerable<ScoreBoard> getGameHighScore(int Gameid)
        {
            var fromDb = (from x in db.ScoreToUserToGames
                                        where x.gameid == Gameid
                                        select x).Take(5);
            List<ScoreBoard> ReturnList = new List<ScoreBoard>();
            foreach (var score in fromDb)
            {
                ScoreBoard Temp = (from x in db.ScoreBoards
                                     where x.id == score.scoreid
                                     select x).Single();
                ReturnList.Add(Temp);
            }
            return ReturnList;

        }
        #endregion

        #region Get Users Highscore
        public IEnumerable<ScoreBoard> getUserHighscore(int UserId)
        {
            var fromDb = (from x in db.ScoreToUserToGames
                          where x.userid == UserId
                          select x).Take(5);
            List<ScoreBoard> Returnlist = new List<ScoreBoard>();
            foreach(var Game  in fromDb)
            {
                ScoreBoard Temp = (from x in db.ScoreBoards
                                     where x.id == Game.scoreid
                                     select x).Single();
                
                Returnlist.Add(Temp);
            }
            return Returnlist;

        }
        #endregion

        #region Create New Score
        public ScoreBoard CreateNewScore()
        {
            //Creating a new ScoreBoard
            ScoreBoard userScore = new ScoreBoard
            {
                win = 0,
                loss = 0,
                ratio = 0,
            };
            //Adding it
            db.ScoreBoards.InsertOnSubmit(userScore);
            db.SubmitChanges();
            return userScore;
        }
        #endregion

        #region Create New User Game Score
        public void CreateNewUserGameScore(int userId, int gameId)
        {
            ScoreBoard userScore = CreateNewScore();
            ScoreToUserToGame scoreRelationship = new ScoreToUserToGame
            {
                scoreid = userScore.id,
                gameid = gameId,
                userid = userId,
            };
            db.ScoreToUserToGames.InsertOnSubmit(scoreRelationship);
            db.SubmitChanges();
        }
        #endregion

        #region Update User GameScore
        public void UpdateUserGameScore(ScoreBoard userNewScore)
        {
            ScoreBoard userOldScore = (from x in db.ScoreBoards
                                       where x.id == userNewScore.id
                                       select x).First();
            userOldScore.loss = userNewScore.loss;
            userOldScore.win = userNewScore.win;
            userOldScore.ratio = userNewScore.ratio;
            db.SubmitChanges();
        }
        #endregion

        #region Remvoe Score
        public void removeScore(ScoreBoard score)
        {
            ScoreToUserToGame scoreRelaionship = (from x in db.ScoreToUserToGames
                                                   where x.scoreid == score.id
                                                    select x).Single();
            db.ScoreToUserToGames.DeleteOnSubmit(scoreRelaionship);
            db.ScoreBoards.DeleteOnSubmit(score);
            db.SubmitChanges();
        }
        #endregion

        #region Check if Score Exists
        public bool checkIfScoreExists(int Userid, int Gameid)
        {
            var check = (from x in db.ScoreToUserToGames
                         where x.userid == Userid && x.gameid == Gameid
                         select x).SingleOrDefault();

            //Simple bool 
            if (check == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        #endregion
    }
}